require(dichromat)
color <- colorschemes$Categorical.12

major.party.labels.HOR <- c("DPJ_HOR", "JCP_HOR", "JSP_HOR", "LDP_HOR")
major.party.labels.HOC <- c("DPJ_HOC", "JCP_HOC", "JSP_HOC", "LDP_HOC")
committee.labels.jp <- c("内閣委員会", "総務委員会", "法務委員会", "外交防衛委員会", 
                         "財務金融委員会", "文部科学委員会", "厚生労働委員会", 
                         "農林水産委員会", "経済産業委員会", "国土交通委員会", 
                         "交通・情報通信・国土・環境委員会", "環境委員会")

# extract committee-specific party positions by year
committee.specific.positions.HOR <- committee.specific.positions.HOC <- array(NA, c(4, 12, 61))
for (i in 1:61) {
  positions <- readRDS(paste0("Wordfish/positions_", 1958 + i, ".rds"))
  for (j in 1:12) {
    if (committee.labels.jp[j] %in% colnames(positions)) {
      for (k in 1:4) {
        if (major.party.labels.HOR[k] %in% rownames(positions)) {
          committee.specific.positions.HOR[k, j, i] <- positions[major.party.labels.HOR[k], committee.labels.jp[j]]
        }
        if (major.party.labels.HOC[k] %in% rownames(positions)) {
          committee.specific.positions.HOC[k, j, i] <- positions[major.party.labels.HOC[k], committee.labels.jp[j]]
        }
      }
    }
  }
}

committee.labels.en <- c("Cabinet", "General Affairs", "Judicial Affairs", 
                         "Foreign Affairs and Defense", 
                         "Financial Affairs", "Education, Culture, and Science", 
                         "Health, Welfare, and Labor", 
                         "Agriculture, Forestry, and Fisheries", 
                         "Economy and Industry", "Land and Transport", 
                         "Trans, Info, Land, and Envir", "Environment")
col.list <- c(color[8], "darkgray", color[10], color[12])

# Figure A7a: committee-specific ideal-point trajectories in the HOR
png("Figure_A7a.png",
    width = 6, height = 4, unit = "in", pointsize = 8, res = 2000)
layout(matrix(1:12, 3, 4, byrow = TRUE))
par(mar = c(1.5, 1.5, 2, 0.2), lwd = 0.5)
for (i in c(1:10, 12)) {
  plot(NULL, NULL, type = "n", xlim = c(1, 61), ylim = c(-3, 3), 
       main = committee.labels.en[i], xlab = "", ylab = "", xaxt = "n", yaxt = "n")
  abline(h = -3:3, lty = 3, col = "gray")
  for (j in 1:4) {
    lines(1:61, committee.specific.positions.HOR[j, i, ], col = col.list[j])
  }
  axis(1, at = seq(1, 61, 10), labels = NA, lwd = 0.5)
  mtext(c(seq(59, 99, 10), "09", "19"), side = 1, line = 0.5, at = seq(1, 61, 10), cex = 0.6)
  axis(2, at = -3:3, labels = NA, lwd = 0.5)
  mtext(-3:3, side = 2, line = 0.5, at = -3:3, cex = 0.6)
}
plot(NULL, NULL, type = "n", bty = "n", xlim = c(0, 1), ylim = c(0, 1), 
     xlab = "", ylab = "", xaxt = "n", yaxt = "n")
legend("center", legend = c("DPJ/DP", "JCP", "JSP/SDP", "LDP"), 
       col = col.list, lty = 1, bty = "n", cex = 1.2)
dev.off()

# Figure A7b: committee-specific ideal-point trajectories in the HOC
png("Figure_A7b.png",
    width = 6, height = 4, unit = "in", pointsize = 8, res = 2000)
layout(matrix(1:12, 3, 4, byrow = TRUE))
par(mar = c(1.5, 1.5, 2, 0.2), lwd = 0.5)
for (i in 1:12) {
  plot(NULL, NULL, type = "n", xlim = c(1, 61), ylim = c(-3, 3), 
       main = committee.labels.en[i], xlab = "", ylab = "", xaxt = "n", yaxt = "n")
  abline(h = -3:3, lty = 3, col = "gray")
  for (j in 1:4) {
    lines(1:61, committee.specific.positions.HOC[j, i, ], col = col.list[j])
  }
  axis(1, at = seq(1, 61, 10), labels = NA, lwd = 0.5)
  mtext(c(seq(59, 99, 10), "09", "19"), side = 1, line = 0.5, at = seq(1, 61, 10), cex = 0.6)
  axis(2, at = -3:3, labels = NA, lwd = 0.5)
  mtext(-3:3, side = 2, line = 0.5, at = -3:3, cex = 0.6)
}
dev.off()
